package com.soccertgc.core.game
{
	import com.soccertgc.utils.ClassUtils;

	public class GameRole
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const IDLE:GameRole						= new GameRole(0, "idle",   "Idle");
		public static const ATTACK:GameRole						= new GameRole(1, "att",    "Attack");
		public static const DEFENCE:GameRole					= new GameRole(2, "def",	"Defence");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameRole>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameRole>();
			_list.push(GameRole.ATTACK);
			_list.push(GameRole.DEFENCE);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _sign:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameRole(id:uint, sign:String, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameRole enum already created!");
			// Init
			_id    = id;
			_name  = name;
			_sign  = sign;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get sign():String
		{
			return _sign;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
				
		/**
		 * Search a GameRole by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameRole constant for the given id, null if no GameRole
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameRole
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameRole for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameRole by given name 
		 * @param name Name of the player GameRole to find
		 * @return PlayerGameRole constant for the given id, null if no GameRole
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameRole
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameRole for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a GameRole by given sign 
		 * @param name sign of the player GameRole to find
		 * @return PlayerGameRole constant for the given id, null if no GameRole
		 * for the given sign has been found
		 */             
		public static function getBySign(sign:String):GameRole
		{
			// If the sign is NULL, force the GameRole IDLE
			if (sign == null)
			{
				return IDLE;
			}
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].sign == sign) return _list[i];
			}
			throw new Error("GameRole for sign '" + sign + "' unknown!");
		}
			
	}
}